// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

module.exports = {
	_before: function () { // 通用预处理器
		this.dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
	},
	// 获取课程分类列表
	async getCourseCategory() {
		return await this.dbJQL.collection('course-category').field('_id as id, name').get()
	},
	// 获取课程列表
	async getCourse(params = {}) {
		const {
			category_id,
			query,
			pageNum=1,
			pageSize=6
		} = params;
		
		let skipNum = pageSize*(pageNum -1);
		
		return await this.dbJQL.collection('course').where({
			category_id,
			title: new RegExp(query, 'i')
		}).skip(skipNum).limit(pageSize).orderBy('create_date desc').get({
			getCount: true
		})
	}
	
}
